package logic

import (
	"context"
	"time"
	"xialou_platform/app/requestx"
	"xialou_platform/app/respx"
	"xialou_platform/app/tools"
	"xialou_platform/services/system/rpc/systemClient"

	"xialou_platform/services/system/api/internal/svc"
	"xialou_platform/services/system/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) UserInfoLogic {
	return UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (resp *types.UserInfoResp, err error) {
	uid, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return nil, err
	}

	sysUser, err := l.svcCtx.SystemRpc.SysUserById(l.ctx, &systemClient.IdBody{
		Id: uid,
	})
	if err != nil {
		return nil, err
	}
	resp = &types.UserInfoResp{
		Id:         respx.IDFrom(sysUser.Id).ToString(),
		Avatar:     sysUser.Avatar,
		Name:       sysUser.Avatar,
		Gender:     sysUser.Gender,
		Phone:      sysUser.Phone,
		Email:      sysUser.Email,
		Role:       "admin",
		DeptName:   "开发部",
		CreateTime: tools.TimeUnixShowLayoutString(sysUser.CreateTime),
		LoginIp:    "0.0.0.0",
		LoginDate:  tools.TimeUnixShowLayoutString(time.Now().Unix()),
	}
	return resp, nil
}
